package com.youpai.platform.vulture.service.handler.flag;

import com.youpai.platform.vulture.common.entity.AccessMessage;
import com.youpai.platform.vulture.common.enums.MsgFlagEnum;
import com.youpai.platform.vulture.service.channel.VultureChannel;
import com.youpai.platform.vulture.service.handler.vulture.MessageHandler;
import io.netty.channel.ChannelHandler;
import org.springframework.stereotype.Component;

/**
 * @date: 2022/7/10 17:42
 * @author: Anna
 * @version: 1.0
 * @description: 心跳请求处理
 */
@Component
@ChannelHandler.Sharable
public class HeartbeatMessageHandler implements MessageHandler {

    @Override
    public void handle(VultureChannel vultureChannel, AccessMessage accessMessage) {
        // 如果是ping ，回复pong
        if (accessMessage.getFlag() == MsgFlagEnum.HEART_BEAT_PING.flag) {
            // 回复pong
            AccessMessage access = AccessMessage.buildPongMessage();
            vultureChannel.send(access);
            return;
        }
        // 如果是pong修改最后一次收到心跳的时间
        vultureChannel.refreshLastReceiveHeartbeatTime();
    }
}
